﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.Models;
using System.Configuration;
using Microhard.WeixinMP.Unities;

namespace Microhard.WeixinMP.Impls
{
    public class WXArticleResponse : BaseResponseImpl
    {
        public override Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand cmm = new WeixinCommand();
                cmm.Event = "CLICK";
                return cmm;
            }
        }

        public override bool IsCallMe(Microhard.WeixinMP.Models.RequestServiceModel model)
        {
            if (model.Event == null)
                return false;
            if (model.Event.Content == null)
                return false;
            string cmd = model.Event.Content.EventKey;

            if (!string.IsNullOrEmpty(cmd))
            {
                MicrohardWXEntities db = new MicrohardWXEntities();
                var cnt = db.Articles.Count(m => m.Type == cmd);
                return cnt > 0 ? true : false;
            }
            return false;
        }


        protected override Models.WeixinBaseData<object> CreateFeatureResponse(Models.RequestServiceModel request, Models.WeixinBaseData<object> response)
        {
            //创建会话成功，发起服务
            response.MsgType = WeixinMsgType.News;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            //获得信息
            MicrohardWXEntities db = new MicrohardWXEntities();

            var msg = db.Articles.Where(i => i.CompanyID == request.WXMPAccountUser.CompanyID
                && i.Type == request.Event.Content.EventKey).OrderByDescending(d => d.CreateTime).Take(8).ToList();

            string url = ConfigurationManager.AppSettings["ArticleDetailUrl"];


            WeixinNews_Response ask = new WeixinNews_Response();
            ask.Items = new List<WeixinNewsItem_Response>();
            foreach (var m in msg)
            {
                var add = new WeixinNewsItem_Response
                {
                    Description = string.Empty,
                    Title = m.Title,
                    PicUrl = string.Empty,
                    Url = url + "?oid=" + request.FromUserName + "&aid=" + m.ArticleID
                };
                var picurl = m.ArticleContent.GetContentRandomImage();
                if (!string.IsNullOrEmpty(picurl))
                {
                    add.PicUrl = ConfigurationManager.AppSettings["BaseUrl"] + picurl;
                }
                ask.Items.Add(add);
            }
            //添加更多连接
            string urlIndex = ConfigurationManager.AppSettings["ArticleIndexUrl"];
            ask.Items.Add(new WeixinNewsItem_Response
            {
                Description = string.Empty,
                Title = "查看更多",
                PicUrl = string.Empty,
                Url = urlIndex + "?companyID=" + request.WXMPAccountUser.CompanyID + "&type=" + request.Event.Content.EventKey
                + "&openID=" + request.FromUserName
            });
            response.Content = ask;
            return response;
        }
    }
}
